/**
 * 
 */
package com.base.myproject.client.busi.b2c;



import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseStoreEvent;
import com.base.myproject.client.busi.BaseStoreListener;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.BaseStoreEvent.EventType;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.ui.BaseFormPanel;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author zgw
 *2009-8-18 下午04:56:52
 * javazgw@gmail.com
 */
public class CcodePanel extends ContentPanel implements com.extjs.gxt.ui.client.event.Listener<ButtonEvent>,AsyncCallback<String>,BaseStoreListener{
	 String ccode ;
	 BaseFormPanel form ;
	 boolean canbechanged=true;
	 BaseStore<JDBCModelData> store = null;
	 TextField<String> ccodefield;
	 TextField<String> cname;
	 TextField<String> loginname ;
	 TextField<String> password;
	 TextField<String> email ;
	 TextField<String> phone ;
	 TextArea addr ;
	public CcodePanel(String ccode)
	{
			this(ccode,false);
		
	}
	public CcodePanel(String ccode,boolean canbechanged)
	{
		this.ccode = ccode;
		this.canbechanged = canbechanged;
		init();
		
	}
	void init()
	{
		
		this.setHeaderVisible(false);
		this.setFrame(false);
	
		add(CreateForm());
	}
	void CreateStore()
	{
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.addField("icode");
		type.addField("ccode");
		type.addField("cname");
		type.addField("email");
		type.addField("password");
		type.addField("loginname");
		type.addField("phone");
		type.addField("addr");

		
		StoreInfo info = new StoreInfo();
		info.setTablename("contract");
		info.setSql("select * from ccode where ccode ='"+getCcode()+"'");

		
		store = new  BaseStore<JDBCModelData> (type,info);
		store.addBaseStoreListener(EventType.LOAD_COMPLETE, this);
		
	}
	BaseFormPanel CreateForm()
	{
		CreateStore();
		 form = new BaseFormPanel(store,false);
		form.setHeaderVisible(false);
		ccodefield = new TextField<String>();
		ccodefield.setName("ccode");
		ccodefield.setAllowBlank(false);
		ccodefield.setFieldLabel("客户编码");
		
		 cname = new TextField<String>();
		cname.setName("cname");
		cname.setAllowBlank(false);
		cname.setFieldLabel("您所在公司全称");
		loginname = new TextField<String>();
		loginname.setName("loginname");
		loginname.setAllowBlank(false);
		loginname.setFieldLabel("登录系统的名称");
		 password = new TextField<String>();
		password.setPassword(true);
		password.setName("password");
		password.setAllowBlank(false);
		password.setFieldLabel("密码");
		 email = new TextField<String>();
		 email.setRegex(".+@.+\\.[a-z]+");
	
		email.getMessages().setRegexText("邮件的格式录入错误");
		email.setName("email");
		email.setAllowBlank(false);

		email.setFieldLabel("邮件地址");

		 phone = new TextField<String>();
		 phone.setName("phone");
		 phone.setAllowBlank(false);
		 phone.setFieldLabel("联系电话");
			
			 addr = new TextArea();
			 addr.setPreventScrollbars(true);
			 addr.setName("addr");
			 addr.setAllowBlank(false);
			 addr.setFieldLabel("收货地址");
			 addr.setMaxLength(250);
		form.add(loginname);
		form.add(password);
		//form.add(ccodefield);
		form.add(cname);
		
		form.add(email);
		form.add(phone);
		FormData fd = new FormData(200, 80);
		form.add(addr,fd);
		form.inited(); //最后绑定数据
		
		if(canbechanged)
		{
		Button save = new Button("保存");

		form.addButton(save);
		save.addListener(Events.Select, this);
		}
	//	save.addSelectionListener(listener)
		form.setBodyBorder(false);
		return form;
	}
	
	
	/**
	 * @return the ccode
	 */
	public String getCcode() {
		return ccode;
	}
	/**
	 * @param ccode the ccode to set
	 */
	public void setCcode(String ccode) {
		this.ccode = ccode;
	}
	/**
	 * @return the store
	 */
	public BaseStore<JDBCModelData> getStore() {
		return store;
	}
	/**
	 * @param store the store to set
	 */
	public void setStore(BaseStore<JDBCModelData> store) {
		this.store = store;
	}
	@Override
	public void handleEvent(ButtonEvent be) {
		// TODO Auto-generated method stub
	
	 if (be.getButton().getText().equals("save"))
		{
		if(!form.isValid() )
		{
			
			return;
		}
			
			final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
			.create(com.base.myproject.client.tools.GreetingService.class);

	//System.out.println(ll.size());
			CcodeObject cco = new CcodeObject("1003");
			//cco.setCcode(ccodefield.getValue());
			cco.setCname(cname.getValue());
			cco.setEmail(email.getValue());
			cco.setLoginname(loginname.getValue());
			cco.setPassword(password.getValue());
			cco.setPhone(phone.getValue());
			cco.setAddr(addr.getValue());
			greetingService.greetServerCcode(cco,this);
			
		}
		
	}
	public void onFailure(Throwable caught) {
		// Show the RPC error message to the user
		System.out.println("error");
	}

	public void onSuccess(String result) {
		if(result.equals(SystemMessage.getMessage(4, 0)))
		{
			Info.display("",SystemMessage.getMessage(4, 0));
		}
	}
	@Override
	public void BaseStoreEventHandle(BaseStoreEvent evt) {
	
		
	}
	
	
}
